// Package ui 提供用户界面相关的功能
package ui

import (
	"fmt"
	"runtime"
	"strings"

	"github.com/dean2027/aish/pkg/config"
	"github.com/fatih/color"
)

// PlatformIcons 跨平台图标配置
type PlatformIcons struct {
	Robot    string
	Sparkles string
	Brain    string
	Rocket   string
	Speech   string
	Warning  string
	Feature  string
	Gear     string
	Book     string
	Arrow    string
	Success  string
}

// GetIcons 根据操作系统返回合适的图标
func GetIcons() PlatformIcons {
	if runtime.GOOS == "windows" {
		// Windows 使用纯ASCII字符
		return PlatformIcons{
			Robot:    "[AI]",
			Sparkles: "*",
			Brain:    "[智]",
			Rocket:   ">>",
			Speech:   "[话]",
			Warning:  "!",
			Feature:  "*",
			Gear:     "[#]",
			Book:     "[?]",
			Arrow:    "->",
			Success:  ">>",
		}
	} else {
		// Unix系统使用emoji
		return PlatformIcons{
			Robot:    "🤖",
			Sparkles: "✨",
			Brain:    "🧠",
			Rocket:   "🚀",
			Speech:   "💬",
			Warning:  "⚠️",
			Feature:  "•",
			Gear:     "🤖",
			Book:     "📖",
			Arrow:    "→",
			Success:  "🚀",
		}
	}
}

// ShowStartupBanner 显示跨平台兼容的启动横幅
func ShowStartupBanner(cfg *config.Config, version string) {
	icons := GetIcons()

	// 顶部横幅
	color.New(color.FgCyan, color.Bold).Println("┌─────────────────────────────────────────────────────────────────┐")
	color.New(color.FgCyan, color.Bold).Println("│                                                                 │")
	color.New(color.FgCyan, color.Bold).Print("│   ")
	color.New(color.FgYellow, color.Bold).Print("   _    ___ ____  _   _   ")
	color.New(color.FgGreen, color.Bold).Printf("%s 智能终端助手", icons.Robot)
	color.New(color.FgCyan, color.Bold).Println("      │")
	color.New(color.FgCyan, color.Bold).Print("│   ")
	color.New(color.FgYellow, color.Bold).Print("  / \\  |_ _/ ___|| | | |  ")
	color.New(color.FgMagenta, color.Bold).Printf("%s %s", icons.Sparkles, version)
	color.New(color.FgCyan, color.Bold).Println("          │")
	color.New(color.FgCyan, color.Bold).Print("│   ")
	color.New(color.FgYellow, color.Bold).Print(" / _ \\  | |\\___ \\| |_| |  ")
	color.New(color.FgBlue).Printf("%s ReAct架构", icons.Brain)
	color.New(color.FgCyan, color.Bold).Println("       │")
	color.New(color.FgCyan, color.Bold).Print("│   ")
	color.New(color.FgYellow, color.Bold).Print("/ ___ \\ | | ___) |  _  |  ")
	color.New(color.FgRed).Printf("%s AI Shell", icons.Rocket)
	color.New(color.FgCyan, color.Bold).Println("        │")
	color.New(color.FgCyan, color.Bold).Print("│   ")
	color.New(color.FgYellow, color.Bold).Print("/_/   \\_\\___|____/|_| |_|  ")
	color.New(color.FgGreen).Printf("%s 智能执行", icons.Speech)
	color.New(color.FgCyan, color.Bold).Println("       │")
	color.New(color.FgCyan, color.Bold).Println("│                                                                 │")
	color.New(color.FgCyan, color.Bold).Println("└─────────────────────────────────────────────────────────────────┘")

	fmt.Println()

	// 系统信息
	showSystemInfo(cfg, icons)

	// 安全警告
	showSecurityWarning(cfg, icons)

	// 使用说明
	showUsageInstructions(icons)

	// 准备就绪提示
	fmt.Println()
	color.New(color.FgGreen, color.Bold).Printf("%s 准备就绪! 请描述您想要完成的任务...\n", icons.Success)
	color.New(color.FgCyan).Println("═══════════════════════════════════════════════════════════════════")
	fmt.Println()
}

// showSystemInfo 显示系统配置信息
func showSystemInfo(cfg *config.Config, icons PlatformIcons) {
	color.New(color.FgWhite, color.Bold).Printf("%s 系统配置\n", icons.Gear)
	color.New(color.FgCyan).Print("   ├─ AI模型: ")
	color.New(color.FgYellow, color.Bold).Println(cfg.Model)
	color.New(color.FgCyan).Print("   ├─ API地址: ")
	color.New(color.FgBlue).Println(cfg.BaseURL)
	color.New(color.FgCyan).Print("   ├─ 执行配置: ")
	color.New(color.FgGreen).Printf("循环=%d次, 历史=%d条\n", cfg.MaxIterations, cfg.MaxHistory)
	color.New(color.FgCyan).Print("   ├─ 超时设置: ")
	color.New(color.FgMagenta).Printf("命令=%ds, 推理=%ds\n", cfg.CommandTimeout, cfg.ReasoningTimeout)

	// 配置文件信息
	if cfg.ConfigFile != "" {
		color.New(color.FgCyan).Print("   └─ 配置文件: ")
		color.New(color.FgHiBlack).Println(cfg.ConfigFile)
	} else {
		color.New(color.FgCyan).Print("   └─ 配置文件: ")
		color.New(color.FgHiBlack).Println("未使用")
	}
}

// showSecurityWarning 显示安全警告
func showSecurityWarning(cfg *config.Config, icons PlatformIcons) {
	if cfg.SkipConfirm {
		fmt.Println()
		color.New(color.FgRed, color.Bold).Printf("%s  警告: ", icons.Warning)
		color.New(color.FgYellow).Println("已启用跳过确认模式 (危险操作将不询问)")
	}
}

// showUsageInstructions 显示使用说明
func showUsageInstructions(icons PlatformIcons) {
	fmt.Println()
	color.New(color.FgWhite, color.Bold).Printf("%s 使用说明\n", icons.Book)

	instructions := []struct {
		command     string
		description string
		color       color.Attribute
	}{
		{"输入任务描述", "AI自动分析并执行", color.FgWhite},
		{"/clear 或 /cls", "清除历史记忆", color.FgYellow},
		{"/exit 或 /quit", "退出程序", color.FgRed},
	}

	for _, instr := range instructions {
		color.New(color.FgCyan).Printf("   %s ", icons.Feature)
		if instr.color == color.FgWhite {
			color.New(instr.color).Print(instr.command)
		} else {
			parts := strings.Split(instr.command, " 或 ")
			if len(parts) == 2 {
				color.New(instr.color).Print(parts[0])
				color.New(color.FgWhite).Print(" 或 ")
				color.New(instr.color).Print(parts[1])
			} else {
				color.New(instr.color).Print(instr.command)
			}
		}
		color.New(color.FgHiBlack).Printf(" %s %s\n", icons.Arrow, instr.description)
	}
}
